﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Collections;
using System.Data.Common;

namespace Cashier_Service
{
    public class BillReshipmentService
    {
        /// <summary>
        /// 查询退货单
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="reshipmentno">退货单号</param>
        /// <returns></returns>
        public BillReshipment GetBillReshipment(long id, String reshipmentno)
        {
            BillReshipment reshipment = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    reshipment = new BillReshipment();
                    reshipment.Id = id;
                    reshipment.Reshipmentno = reshipmentno;
                    reshipment = new BillReshipmentDAO().GetBillReshipment(conn, reshipment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return reshipment;
        }

        /// <summary>
        /// 查询退货单列表
        /// </summary>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">页面数据量</param>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="reshipmentno">退货单号</param>
        /// <param name="shipmentno">出货单号</param>
        /// <param name="memberno">客户编号</param>
        /// <param name="adminUsername">退货人</param>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="ascfield">升序字段</param>
        /// <param name="descfield">降序字段</param>
        /// <returns></returns>
        public DataTable GetBillReshipmentTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, String reshipmentno, String shipmentno,
            String memberno, String adminUsername, String startdate, String enddate, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillReshipment reshipment = new BillReshipment();
                    reshipment.Pageindex = pageindex;
                    reshipment.Pagesize = pagesize;
                    reshipment.EnterpriseNo = enterpriseNo;
                    reshipment.StoreNo = storeNo;
                    reshipment.Reshipmentno = reshipmentno;
                    reshipment.Shipmentno = shipmentno;
                    reshipment.Memberno = memberno;
                    reshipment.AdminUsername = adminUsername;
                    reshipment.StartDate = startdate;
                    reshipment.EndDate = enddate;
                    reshipment.Ascfield = ascfield;
                    reshipment.Descfield = descfield;
                    dt = new BillReshipmentDAO().GetBillReshipmentTable(conn, reshipment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询退货单记录数
        /// </summary>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="reshipmentno">退货单号</param>
        /// <param name="shipmentno">出货单号</param>
        /// <param name="memberno">客户编号</param>
        /// <param name="adminUsername">退货人</param>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <returns></returns>
        public long GetBillReshipmentRecord(String enterpriseNo, String storeNo, String reshipmentno, String shipmentno,
            String memberno, String adminUsername, String startdate, String enddate)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillReshipment reshipment = new BillReshipment();
                    reshipment.EnterpriseNo = enterpriseNo;
                    reshipment.StoreNo = storeNo;
                    reshipment.Reshipmentno = reshipmentno;
                    reshipment.Shipmentno = shipmentno;
                    reshipment.Memberno = memberno;
                    reshipment.AdminUsername = adminUsername;
                    reshipment.StartDate = startdate;
                    reshipment.EndDate = enddate;
                    record = new BillReshipmentDAO().GetBillReshipmentRecord(conn, reshipment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入退货单
        /// </summary>
        /// <param name="reshipment">退货单</param>
        /// <param name="list">商品集合</param>
        /// <returns></returns>
        public long InsertBillReshipment(BillReshipment reshipment, ArrayList list)
        {
            long id = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    conn.Open();
                    trans = conn.BeginTransaction();
                    id = new BillReshipmentDAO().InsertBillReshipment(conn, reshipment);
                    if (id > 0)
                    {
                        new BillReshipmentGoodsService().InsertBillReshipmentGoods(conn, list);
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    if (trans != null)
                        trans = null;
                }
            }
            return id;
        }

        /// <summary>
        /// 更新退货单
        /// </summary>
        /// <param name="reshipment"></param>
        /// <returns></returns>
        public int UpdateBillReshipment(BillReshipment reshipment)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new BillReshipmentDAO().UpdateBillReshipment(conn, reshipment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除退货单及商品
        /// </summary>
        /// <param name="reshipmentnos">退货单号</param>
        /// <returns></returns>
        public int DeleteBillReshipment(String reshipmentnos)
        {
            int count = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    if (!String.IsNullOrEmpty(reshipmentnos))
                    {
                        conn.Open();
                        trans = conn.BeginTransaction();
                        String[] arr = reshipmentnos.Split(new Char[] { ',' });
                        BillReshipmentDAO reshipmentDAO = new BillReshipmentDAO();
                        BillReshipment reshipment = new BillReshipment();
                        BillReshipmentGoodsService reshipmentGoodsService = new BillReshipmentGoodsService();
                        foreach (String lopReshipmentno in arr)
                        {
                            reshipment.Reshipmentno = lopReshipmentno;
                            int flag = reshipmentDAO.DeleteBillReshipment(conn, reshipment);
                            if (flag > 0)
                            {
                                reshipmentGoodsService.DeleteBillReshipmentGoods(conn, lopReshipmentno);
                                count++;
                            }
                        }
                        trans.Commit();
                    }
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    if (trans != null)
                        trans = null;
                }
            }
            return count;
        }
    }
}
